<?php
	abstract class AbstractImport extends AppModel
	{
		var $actsAs = array();
		
		var $useTable = false;
		
		var $fieldProperties = array("file"=>array("type"=>"file",'label'=>"Csv file",'group'=>array("import")),'has_headers'=>array("type"=>"checkbox",'group'=>array("import")));
		
		function afterInitialize()
		{
			$this->Behaviors->attach("Cms.Upload",array("file" => array("dir"=>$this->getCsvUploadPath(),"empty"=>false,
			"mediaTypes"=>array("Text"=>array("allowedExt"=>array("csv"),"allowedMime"=>array("text/*","application/vnd.ms-excel"))))));
			parent::afterInitialize();
		}
		
		/**
		 * return false only.By this only file will get moved.
		 * @see AppModel::beforeSave()
		 */
		function beforeSave($options)
		{
			return false;
		}
		/**
		 * Get's the model on which import will be performed.
		 * return @var AppModel
		 */
		abstract function getImporterModel();
		
		function getImportFields()
		{
			return $this->getImporterModel()->getFields("group","import");
		}
		
		function getImportRequiredFields()
		{
			$importFields = $this->getImportFields();
			$requiredFields = $this->getImporterModel()->getFields("required","1");
			$requiredFields = array_intersect($importFields,$requiredFields);
			return $requiredFields;
		}
		
		function saveToImporterModel($data)
		{
			foreach($data as $field => $value)
			{
				$data[$field] = $this->setFieldData($field, $value);
			}
			$this->getImporterModel()->create();
			$data = array($this->getImporterModel()->alias => $data);
			$save = $this->getImporterModel()->save($data);
			if($save)
			{
				$return = true;
			}else
			{
				$return = $this->getImporterModel()->validationErrors;
			}
			return $return;
		}
		
		function getImportFieldsAsOtpions()
		{
			$options = array();
			foreach($this->getImportFields() as $field)
			{
				$options[$field] = $this->getImporterModel()->getFieldProperty($field,"label");
			}
			return $options;
		}
		
		protected function setFieldData($field,$value)
		{
			$fieldType = $this->getImporterModel()->getFieldProperty($field,"type");
			$multiValueFields = array("multipleSelect","multipleCheckbox");
			$selectFields = array("select","radio");
			if(in_array($fieldType,$multiValueFields))
			{
				$options = $this->getImporterModel()->getFieldProperty($field,"options");
				$values = explode(",",$value);
				$value = array();
				foreach($values as $fieldValue)
				{
					$valueKey = array_search($fieldValue,$options,false);
					if($valueKey !==false)
					{
						$value[] = $valueKey;
					}
				}
			}else if(in_array($fieldType,$selectFields))
			{
				$options = $this->getImporterModel()->getFieldProperty($field,"options");
				$value = array_search($value,$options,false);
			}
			return $value;
		}
				
		function getCsvUploadPath()
		{
			return WWW_ROOT.DS."uploads".DS."import_csv".DS;
		}        
		/**
		 * Config to allow use a file uploaded on server rather than browser.
		 */
		function allowLocalFiles()
		{
			return true;
		}
		
		function getLocalCsvFiles()
		{
			$localPath = WWW_ROOT.DS.'csv'.DS;
			$files = array();
			if(!is_dir($localPath))
			{
				return $files;
			}
			$Directory = new DirectoryIterator($localPath);
			foreach($Directory as $csvFile)
			{
				$fileName = $csvFile->getFilename();
				$extension = array_pop(explode(".",$fileName));
				$extension = low($extension);
				if(!$csvFile->isFile() || $extension != "csv")
				{
					continue;
				}
				$files[] = $fileName; 
			}
			return $files;
		}
		//copy local Server copy to upload Path
		function copyLocalFile($file)
		{
			$uploadPath = $this->getCsvUploadPath();
			if(!is_dir($uploadPath))
			{
				mkdir($uploadPath,0777);
			}
			$localPath = WWW_ROOT.DS.'csv'.DS;
			$localFile = $localPath.$file;
			$uploadFilePath = $uploadPath.$file;
			if(file_exists($uploadFilePath))
			{
				$fileParts = explode(".",$file);
				$extension = array_pop($fileParts);
				$file = implode('.',$fileParts);
				$file .= "_".time().'.'.$extension;
				$uploadFilePath = $uploadPath.$file;
			}
			copy($localFile,$uploadFilePath);
			return $uploadFilePath;
		}
		
		function addNewImportFile($filePath,$information)
		{
			$CsvFileModel = $this->getFileModel();
			$data = array('full_path'=>$filePath,'filename'=>basename($filePath),'other_information'=>$information);
			$CsvFileModel->create();
			$CsvFileModel->save(array($CsvFileModel->alias => $data));
			return $CsvFileModel->id;
		}
		
		final function getFileModel()
		{
			return cri("Import.CsvImportFile");
		}
	}
?>